import json
import os
basic_path = str(os.path.dirname(__file__)) + "\\"
major_path = basic_path + "..\\..\\major\\major.json"
majorMap_path = basic_path + "json/majorMap.json"

# 打开 JSON 文件
with open(major_path, 'r', encoding='utf-8') as f:
    data = json.load(f)
major_basic = {}
for key, value in data.items():
    key_new = value['code'][0:6]
    major_basic[key_new] = {}
    major_basic[key_new]['name'] = value['name']
    major_basic[key_new]['hot'] = value['heat'].get('view_total', 0)
    major_basic[key_new]['year'] = value['limit_year']
    major_basic[key_new]['degree'] = value['degree']
with open(majorMap_path, 'r', encoding='utf-8') as f:
    data_map = json.load(f)

json_update = {}
for key, value in data_map.items():
    for key1, value1 in value.items():
        for key2, value2 in value1.items():
            json_update[key2] = {}
            json_update[key2]['name'] = major_basic.get(key2, {}).get('name', '未知')
            json_update[key2]['hot'] = major_basic.get(key2, {}).get('hot', '未知')
            json_update[key2]['year'] = major_basic.get(key2, {}).get('year', '未知')
            json_update[key2]['degree'] = major_basic.get(key2, {}).get('degree', '未知')
            json_update[key2]['belong'] = key1
            json_update[key2]['level'] = key

with open('json/majorBasics.json', 'w', encoding='utf-8') as f:
    json.dump(json_update, f, ensure_ascii=False, indent=4)
